package com.eascs.admin.sys.permission.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.internal.util.BeanUtils;
import com.eascs.admin.o2o.loan.action.O2oSearchCustemer;
import com.eascs.admin.sys.permission.form.OrgUserRoleForm;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.ActOrgRoleUserEntity;
import com.eascs.workflow.common.entity.sys.SysRole;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;

@RestController
public class UserRoleRestController extends BaseRestController{
	@Reference
	private SysPermissionService sysPermissionService;
	@Reference
	private SysOrgService sysOrgService;
	
	@RequestMapping("/act/getLeafNode")
	public BaseRestResult<List<ActOrgEntity>> getOrgList(String orgId){
		List<ActOrgEntity> dataList = new ArrayList<>();
		ModelResult<List<ActOrgEntity>> modelResult = sysPermissionService.getLeafNodeByParentId(orgId);
		if(modelResult.isSuccess()){
			dataList = modelResult.getModel();
		}
		return BaseRestResult.buildRestResult(dataList);
	}
	
	@RequestMapping("/act/getOrgList")
	public BaseRestResult<List<ActOrgEntity>> getOrgList(String orgId, int level){
		List<ActOrgEntity> dataList = new ArrayList<>();
		ModelResult<List<ActOrgEntity>> modelResult = sysPermissionService.getOrgListByLevel(orgId, level);
		if(modelResult.isSuccess()){
			dataList = modelResult.getModel();
		}
		return BaseRestResult.buildRestResult(dataList);
	}
	
	@RequestMapping("/act/getRoleLevel")
	public BaseRestResult<String> getRoleLevel(String roleId){
		ModelResult<SysRole> modelResult = sysPermissionService.getRoleById(roleId);
		if(modelResult.isSuccess()){
			return BaseRestResult.buildRestResult(modelResult.getModel().getRoleLevel()+"");
		}else{
			return BaseRestResult.warn(modelResult.getErrMsg());
		}
	}
	
	@RequestMapping("/act/userRole/save")
	public BaseRestResult<String> saveUserRole(OrgUserRoleForm orgUserRoleForm){
		ActOrgRoleUserEntity orgRoleUserEntity = new ActOrgRoleUserEntity();
		BeanUtils.copyProperties(orgUserRoleForm, orgRoleUserEntity);
		User user = UserHolder.getUser();
		if(StringUtil.isNull(orgRoleUserEntity.getId())){
			orgRoleUserEntity.setCreator(user.getAccount());
			orgRoleUserEntity.setCreateTime(new Date());
		}else{
			orgRoleUserEntity.setUpdator(user.getAccount());
			orgRoleUserEntity.setUpdateTime(new Date());
		}
		ModelResult<Integer> modelResult = sysPermissionService.saveOrUpdateOrgUserRole(orgRoleUserEntity);
		if(modelResult.isSuccess()){
			int result = modelResult.getModel();
			if(result > 0){
				return BaseRestResult.warn("保存成功").andRefresh();
			}else{
				return BaseRestResult.warn("保存失败!");
			}
		}else{
			return BaseRestResult.warn(modelResult.getErrMsg());
		}
	}
	
	@RequestMapping("/act/userRole/delete/confirm-{id}")
	public BaseRestResult<String> delUserRoleConfirm(@PathVariable String id){
		BaseRestResult<String> result = BaseRestResult.confirm("确认删除这条数据？").andActionAjaxLoad("/act/userRole/delete-"+id);
		return result;
	}
	
	@RequestMapping("/act/userRole/delete-{id}")
	public BaseRestResult<String> delUserRole(@PathVariable String id){
		ModelResult<Integer> modelResult = sysPermissionService.delUserRole(id);
		
		if(modelResult.isSuccess()){
			int result = modelResult.getModel();
			if(result > 0){
				return BaseRestResult.refresh();
			}else{
				return BaseRestResult.warn("删除失败!");
			}
		}else{
			return BaseRestResult.warn(modelResult.getErrMsg());
		}
	}
	
	/**
	 * 联想经营平台
	 * 
	 * @param o2oSearchUnits
	 * @return
	 */
	@RequestMapping(value = "/act/org/search")
	public BaseRestResult<List<Object[]>> units(O2oSearchCustemer o2oSearchCustemer) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(o2oSearchCustemer.getName())) {
//			data = o2oLoanapplyService.getUnitsList(o2oSearchCustemer.getName());
			ModelResult<List<Object[]>> da = sysOrgService.getOrgListLikeName(o2oSearchCustemer.getName(), null,UserHolder.getUser());
			data = da.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	/**
	 * 联想员工
	 * 
	 * @param o2oSearchUnits
	 * @return
	 */
	@RequestMapping(value = "/act/lenovo/search/employee")
	public BaseRestResult<List<Object[]>> employee(String empName) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(empName)) {
			ModelResult<List<Object[]>> da = sysPermissionService.getLikeEmployeeList(empName);
			data = da.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
}
